/**
 * diy自定义组件
 * */
export default {
  namespaced: true,
  state: {
    configName: '',
    // 已知组件列表默认数据 对象
    defaultArray: {},

    // 页面列表默认数据
    defaultPageArray: [],

    // 默认选中页面的索引
    defaultSelectPageIndex: 0
  },
  mutations: {
    // 页面增加信息
    ADDPAGEARRAY(state, data) {
      state.defaultPageArray.push(data)
    },
    // 页面赋值信息
    SETPAGEARRAY(state, data) {
      state.defaultPageArray = data
    },
    // 修改-默认选中页面的索引
    SETPAGEINDEX(state, data) {
      state.defaultSelectPageIndex = data
    },
    /**
     * @description 默认配置push到数组里面
     * @param {Object} state vuex state
     * @param {Object} data
     * 把默认数据添加到默认数组里面，解耦重复组件公用一条配置的问题
     */
    ADDARRAY(state, data) {
      state.defaultArray[data.num] = data.val
    },
    /**
     * @description 删除列表第几个默认数据
     * @param {Object} state vuex state
     * @param {Object} data 数据
     */
    DELETEARRAY(state, data) {
      let tempObj = delete state.defaultArray[data.num]
        // state.defaultArray.splice(index, 1)
      // 同时把defaultPageArray.val 里的数据也删除
      delete state.defaultPageArray[state.defaultSelectPageIndex].val[data.num]
    },
    /**
     * @description 数组排序
     * @param {Object} state vuex state
     * @param {Object} data 位置index记录
     */
    defaultArraySort(state, data) {

      let newArr = objToArr(state.defaultArray)
      let sortArr = []
      let newObj = {}

      function objToArr(data) {
        let obj = Object.keys(data);
        let m = obj.map(key => data[key]);
        return m
      }

      function swapArray(arr, index1, index2) {
        arr[index1] = arr.splice(index2, 1, arr[index1])[0];
        return arr;
      }
      if (data.oldIndex != undefined) {
        sortArr = JSON.parse(JSON.stringify(swapArray(newArr, data.newIndex, data.oldIndex)))
      } else {
        newArr.splice(data.newIndex, 0, data.element.data().defaultConfig);
        sortArr = JSON.parse(JSON.stringify(newArr))
      }
      for (let i = 0; i < sortArr.length; i++) {
        newObj[sortArr[i].timestamp] = sortArr[i];
      }
      state.defaultArray = newObj;
    },
    /**
     * @description 更新数组某一组数据
     * @param {Object} state vuex state
     * @param {Object} data
     */
    UPDATEARR(state, data) {
      console.log('defaultPageArray', state.defaultPageArray)
        // 处理一下组件的宽、高、左、上
      console.log('data.val.position', data.val.position)
      if (data.val.position) {
        data.val.layoutConfig.height = data.val.position.height;
        data.val.layoutConfig.width = data.val.position.width;
        data.val.layoutConfig.left = data.val.position.left;
        data.val.layoutConfig.top = data.val.position.top;
      }


      state.defaultArray[data.num] = data.val
      state.defaultPageArray[state.defaultSelectPageIndex].val[data.num] = data.val
      let value = Object.assign({}, state.defaultArray);
      state.defaultArray = value;
    },
    /**
     * @description 保存组件名称
     * @param {Object} state vuex state
     * @param {string} data
     */
    SETCONFIGNAME(state, name) {
      state.configName = name;
    },
    /**
     * @description 默认组件清空
     * @param {Object} state vuex state
     * @param {string} data
     */
    SETEMPTY(state, name) {
      state.defaultArray = {};
    }
  },
  actions: {
    getData({ commit }, data) {}
  }
}